import 'dart:convert';

class BuildingPassword {
  final String buildingNumber; // 单元楼号
  final String password; // 密码
  final String? notes; // 备注

  BuildingPassword({
    required this.buildingNumber,
    required this.password,
    this.notes,
  });

  Map<String, dynamic> toJson() => {
        'buildingNumber': buildingNumber,
        'password': password,
        'notes': notes,
      };

  factory BuildingPassword.fromJson(Map<String, dynamic> json) => BuildingPassword(
        buildingNumber: json['buildingNumber'] as String,
        password: json['password'] as String,
        notes: json['notes'] as String?,
      );
}

class CommunityInfo {
  final String id;
  final String name;           // 小区名称
  final String address;        // 小区地址
  final bool allowDelivery;    // 是否允许外卖电瓶车进入
  final bool hasDoorControl;   // 是否有门禁
  final bool hasElevator;      // 是否有电梯
  final bool elevatorNeedCard; // 电梯是否需要刷卡
  final bool isInYuyaoJiangnanSouth; // 是否在余姚江南部
  final bool isInNingciEastNorth; // 是否在宁慈东路北部
  final bool isInHongtang;     // 是否在洪塘
  final bool isNearMixc;       // 是否在万象城附近
  final bool isEasyDelivery;   // 是否好送
  final bool isHardDelivery;   // 是否难送
  final bool isBusiness;       // 是否商家
  final bool hasDeliveryCabinet; // 是否有外卖柜
  final String? notes;          // 备注信息
  final DateTime createTime;   // 创建时间
  final DateTime updateTime;   // 更新时间
  final List<BuildingPassword> buildingPasswords; // 单元楼密码列表
  final String? province;
  final String? city;
  final String? district;
  final double? latitude;      // 纬度
  final double? longitude;     // 经度

  CommunityInfo({
    required this.id,
    required this.name,
    required this.address,
    this.allowDelivery = false,
    this.hasDoorControl = false,
    this.hasElevator = false,
    this.elevatorNeedCard = false,
    this.isInYuyaoJiangnanSouth = false,
    this.isInNingciEastNorth = false,
    this.isInHongtang = false,
    this.isNearMixc = false,
    this.isEasyDelivery = false,
    this.isHardDelivery = false,
    this.isBusiness = false,
    this.hasDeliveryCabinet = false,
    this.notes,
    required this.createTime,
    required this.updateTime,
    this.buildingPasswords = const [],
    this.province,
    this.city,
    this.district,
    this.latitude,
    this.longitude,
  });

  // 从JSON转换为对象
  factory CommunityInfo.fromJson(Map<String, dynamic> json) {
    return CommunityInfo(
      id: json['id'] as String,
      name: json['name'] as String,
      address: json['address'] as String,
      allowDelivery: json['allowDelivery'] as bool? ?? false,
      hasDoorControl: json['hasDoorControl'] as bool? ?? false,
      hasElevator: json['hasElevator'] as bool? ?? false,
      elevatorNeedCard: json['elevatorNeedCard'] as bool? ?? false,
      isInYuyaoJiangnanSouth: json['isInYuyaoJiangnanSouth'] as bool? ?? false,
      isInNingciEastNorth: json['isInNingciEastNorth'] as bool? ?? false,
      isInHongtang: json['isInHongtang'] as bool? ?? false,
      isNearMixc: json['isNearMixc'] as bool? ?? false,
      isEasyDelivery: json['isEasyDelivery'] as bool? ?? false,
      isHardDelivery: json['isHardDelivery'] as bool? ?? false,
      isBusiness: json['isBusiness'] as bool? ?? false,
      hasDeliveryCabinet: json['hasDeliveryCabinet'] as bool? ?? false,
      notes: json['notes'] as String?,
      createTime: DateTime.parse(json['createTime'] as String),
      updateTime: DateTime.parse(json['updateTime'] as String),
      buildingPasswords: (json['buildingPasswords'] as List<dynamic>?)
              ?.map((e) => BuildingPassword.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
      province: json['province'] as String?,
      city: json['city'] as String?,
      district: json['district'] as String?,
      latitude: json['latitude'] as double?,
      longitude: json['longitude'] as double?,
    );
  }

  // 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'address': address,
      'allowDelivery': allowDelivery,
      'hasDoorControl': hasDoorControl,
      'hasElevator': hasElevator,
      'elevatorNeedCard': elevatorNeedCard,
      'isInYuyaoJiangnanSouth': isInYuyaoJiangnanSouth,
      'isInNingciEastNorth': isInNingciEastNorth,
      'isInHongtang': isInHongtang,
      'isNearMixc': isNearMixc,
      'isEasyDelivery': isEasyDelivery,
      'isHardDelivery': isHardDelivery,
      'isBusiness': isBusiness,
      'hasDeliveryCabinet': hasDeliveryCabinet,
      'notes': notes,
      'createTime': createTime.toIso8601String(),
      'updateTime': updateTime.toIso8601String(),
      'buildingPasswords': buildingPasswords.map((p) => p.toJson()).toList(),
      'province': province,
      'city': city,
      'district': district,
      'latitude': latitude,
      'longitude': longitude,
    };
  }

  // 创建副本并更新某些字段
  CommunityInfo copyWith({
    String? id,
    String? name,
    String? address,
    bool? allowDelivery,
    bool? hasDoorControl,
    bool? hasElevator,
    bool? elevatorNeedCard,
    bool? isInYuyaoJiangnanSouth,
    bool? isInNingciEastNorth,
    bool? isInHongtang,
    bool? isNearMixc,
    bool? isEasyDelivery,
    bool? isHardDelivery,
    bool? isBusiness,
    bool? hasDeliveryCabinet,
    String? notes,
    DateTime? createTime,
    DateTime? updateTime,
    List<BuildingPassword>? buildingPasswords,
    String? province,
    String? city,
    String? district,
    double? latitude,
    double? longitude,
  }) {
    return CommunityInfo(
      id: id ?? this.id,
      name: name ?? this.name,
      address: address ?? this.address,
      allowDelivery: allowDelivery ?? this.allowDelivery,
      hasDoorControl: hasDoorControl ?? this.hasDoorControl,
      hasElevator: hasElevator ?? this.hasElevator,
      elevatorNeedCard: elevatorNeedCard ?? this.elevatorNeedCard,
      isInYuyaoJiangnanSouth: isInYuyaoJiangnanSouth ?? this.isInYuyaoJiangnanSouth,
      isInNingciEastNorth: isInNingciEastNorth ?? this.isInNingciEastNorth,
      isInHongtang: isInHongtang ?? this.isInHongtang,
      isNearMixc: isNearMixc ?? this.isNearMixc,
      isEasyDelivery: isEasyDelivery ?? this.isEasyDelivery,
      isHardDelivery: isHardDelivery ?? this.isHardDelivery,
      isBusiness: isBusiness ?? this.isBusiness,
      hasDeliveryCabinet: hasDeliveryCabinet ?? this.hasDeliveryCabinet,
      notes: notes ?? this.notes,
      createTime: createTime ?? this.createTime,
      updateTime: updateTime ?? this.updateTime,
      buildingPasswords: buildingPasswords ?? this.buildingPasswords,
      province: province ?? this.province,
      city: city ?? this.city,
      district: district ?? this.district,
      latitude: latitude ?? this.latitude,
      longitude: longitude ?? this.longitude,
    );
  }
} 